﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaiGaoQiaoWeb.JsonEntities;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using DAO.WaiGaoQiao;
using System.Data;
using System.ComponentModel;
using System.Reflection;
using AutoMapper;
using System.Text;


namespace WaiGaoQiaoWeb.Controllers
{
    public class BringArticleController : Controller
    {
        Work_ArticleBringInfo bring = new Work_ArticleBringInfo();
        Work_Person_Unit_PostInfo unit = new Work_Person_Unit_PostInfo(true);
        Work_ArticleInfo wa = new Work_ArticleInfo();
        Work_post wp = new Work_post();
        Work_ArticleUnit un = new Work_ArticleUnit();
        Work_ArticleRuleInfo ru = new Work_ArticleRuleInfo(true);

        // GET: /BringArticle/
        #region ------------------物品领用----------------------------
        public ActionResult BringIndex()
        {
            return View();
        }
       
        /// <summary>
        /// 转到打印界面
        /// </summary>
        /// <returns></returns>
        public ActionResult PrintPage(string query)
        {
            string condition = string.Empty;
            if (!string.IsNullOrEmpty(query))
            {
                condition = query;
            }
            string[] con = condition.Split(',');
            ViewBag.personName = con[1];
            ViewBag.personNo = con[0];
            ViewBag.cardNo = con[2];
            ViewBag.beginTime = con[3];
            ViewBag.endTime = con[4];
            ViewBag.query = condition;
            return View();
        }
        public ActionResult GetPrintList(PageVM pvm)
        {

            IList<Work_ArticleBringInfo> list = bring.GetAllWork_ArticleBringList(pvm.query);
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = list.Count;
            List<string> v;
            foreach (Work_ArticleBringInfo ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(wa.GetCon_ArticleById(ida.Article_ID).ArticleName);
                v.Add(un.GetCon_ArticleUnitById(Convert.ToInt32(string.IsNullOrEmpty(ida.Article_Unit) ? "0" : ida.Article_Unit)).Unit);
                v.Add(ida.BringTime.ToShortDateString());
                v.Add(ida.BringCount.ToString());
                v.Add(ida.PersonNo.ToString());
                v.Add(ida.PersonName.ToString());

                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 获取已领用的物品
        /// </summary>
        /// <returns></returns>
        public ActionResult GetBringList(PageVM pvm)
        {
            string condition = string.Empty;
            if (!string.IsNullOrEmpty(pvm.query))
            {
                condition = pvm.query;
            }

            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = bring.GetWork_ArticleBringCount(condition);
            IList<Work_ArticleBringInfo> list = bring.GetAllWork_ArticleBring(condition, pvm.rp, pvm.page);
            List<string> v;
            foreach (Work_ArticleBringInfo ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(wa.GetCon_ArticleById(ida.Article_ID).ArticleName);
                v.Add(un.GetCon_ArticleUnitById(Convert.ToInt32(string.IsNullOrEmpty(ida.Article_Unit) ? "0" : ida.Article_Unit)).Unit);
                v.Add(ida.BringTime.ToShortDateString());
                v.Add(ida.BringCount.ToString());
                v.Add(ida.PersonNo.ToString());
                v.Add(ida.PersonName.ToString());

                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 转到物品领用
        /// </summary>
        /// <param name="personNo">用户编号</param>
        /// <param name="personName">用户姓名</param>
        /// <returns></returns>
        public ActionResult AddBring(string personNo, string personName)
        {
            BringVM vm = new BringVM();
            vm.BringTime = DateTime.Now;
            vm.PersonName = string.IsNullOrEmpty(personName) ? "" : personName;
            vm.PersonNo = string.IsNullOrEmpty(personNo) ? "" : personNo;

            return View(vm);
        }

        /// <summary>
        /// 获取还可以领用的物资
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public ActionResult GetUserBring(PageVM pv)
        {
            string personNo = string.Empty;
            string query = string.Empty;
            string queryUnit = string.Empty;
            if (!string.IsNullOrEmpty(pv.query))
            {
                string[] condition = pv.query.Split(',');
                if (!string.IsNullOrEmpty(condition[0]))
                {
                    personNo = condition[0];
                }
                else if (!string.IsNullOrEmpty(condition[1]))
                {
                    personNo = bring.RtnSerial(condition[1]);
                }
            }
            IList<Work_ArticleBringInfo> unitList = bring.GetBringUser(personNo, "");
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pv.page;
            jsonFlexiGridData.total = unitList.Count;
            List<string> v;
            foreach (Work_ArticleBringInfo ida in unitList)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.Article_ID.ToString());
                v.Add(wa.GetCon_ArticleById(ida.Article_ID).ArticleName);
                v.Add(ida.Article_Unit);
                v.Add(un.GetCon_ArticleUnitById(Convert.ToInt32(string.IsNullOrEmpty(ida.Article_Unit) ? "0" : ida.Article_Unit)).Unit);
                v.Add(ida.BringCount.ToString());
                v.Add(ida.postid.ToString());
                v.Add(wp.GetPostName(ida.postid));
                v.Add(ida.clu);
                v.Add(ru.GetAllArticleRule(Convert.ToInt32(ida.clu)).ArticleName);
                v.Add(ida.PersonNo);
                v.Add(ida.PersonName);
                v.Add(GetBringTime(ida.PersonNo, ida.Article_ID.ToString(), ida.Article_Unit).ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);


        }
        /// <summary>
        /// 获取上次领取物品的时间
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="article_id"></param>
        /// <param name="article_unit"></param>
        /// <returns></returns>
        public DateTime GetBringTime(string personNo, string article_id, string article_unit)
        {
            DateTime br = bring.GetMaxBringTime(" AND [PersonNo]='" + personNo + "' AND [Article_ID]='" + article_id + "' AND [Article_Unit]='" + article_unit + "'");
            DateTime reTime = DateTime.MinValue;
            if (br.ToString("yyyy-MM-dd").Equals(DateTime.MinValue.ToString("yyyy-MM-dd")))
            {
                reTime = bring.GetRegTime(personNo);
                return reTime;
            }
            else
            {
                return br;
            }
        }
        /// <summary>
        /// 保存物品领用
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveBring(BringVM vm)
        {
            if (ModelState.IsValid)
            {
                Work_ArticleBringInfo info = new Work_ArticleBringInfo();
                Mapper.DynamicMap(vm, info);
                info.PersonNo =!string.IsNullOrEmpty(vm.HidePersonNo)? bring.RtnPidForPersonNo(vm.HidePersonNo).ToString():vm.PersonNo;
                info.SaveWork_ArticleBring(info);
                return Json(true);
               
            }
            else
            {
                return Json(false);
            }


        }
        /// <summary>
        /// 用来验证是否领取过
        /// </summary>
        /// <param name="postid">领取规则id</param>
        /// <param name="clu">领取规则列</param>
        /// <param name="personNo">工号</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ValidateBring(int? postid, string clu, string personNo, string article_ID, string article_unit)
        {
            Work_ArticleBringInfo workBring = bring.GetWork_ArticleBring(" AND PERSONNO='" + bring.RtnPidForPersonNo(personNo) + "' AND ARTICLE_ID='" + article_ID + "' AND ARTICLE_UNIT='" + article_unit + "'");
            DateTime? bringTime = DateTime.MinValue;//上次领用的日期
            if (workBring != null)
                bringTime = workBring.BringTime;
            DateTime now = DateTime.Now;//获取当前日期
            TimeSpan span = now - bringTime.Value;//时间间隔
            int month = Convert.ToInt32(clu);//获取间隔的月
            int monthdays = 0;//间隔的天数
            int spandays = DateTime.DaysInMonth(bringTime.Value.Year, bringTime.Value.Month) - bringTime.Value.Day;//当月剩下的天数
            if (span.Days > 0)
            {
                for (int i = 1; i < month; i++)
                {
                    monthdays += DateTime.DaysInMonth(bringTime.Value.AddMonths(i).Year, bringTime.Value.AddMonths(i).Month);
                }
                if (span.Days - monthdays - spandays + 1 > 0)
                {
                    return Json(true);
                }
                else
                {
                    return Json(false);
                }
            }
            else
            {
                return Json(false);
            }




        }
        

# endregion 


        #region -------------------数据录入----------------
        public ActionResult InputIndex()
        {
            ViewBag.AID = GetQueryTime(null);
            ViewBag.UnitList = GetUnitList(null);
            ViewBag.clu = GetArticleTime();
            ViewBag.postid = GetPost();
            BringVM vm = new BringVM();
           

            return View(vm);

            
        }
        public IList<SelectListItem> GetPost() 
        {
            SelectListItem sl = new SelectListItem();
            sl.Selected = true;
            sl.Text = "请选择...";
            sl.Value = "0";
            IList<Work_post> list = wp.GetAllWork_Post("");
            IList<SelectListItem> selectList =
             list.Select(m => new SelectListItem() { Text = m.StructrueName.ToString(), Value = m.StructrueId.ToString() }).ToList();
            selectList.Insert(0, sl);
            return selectList;
        
        }
        public IList<SelectListItem> GetArticleTime()
        {
            SelectListItem sl = new SelectListItem();
            sl.Selected = true;
            sl.Text = "请选择...";
            sl.Value = "0";
            IList<Work_ArticleRuleInfo> list = ru.GetAllArticleRule("");
            IList<SelectListItem> selectList =
             list.Select(m => new SelectListItem() { Text = m.ArticleName.ToString(), Value = m.ID.ToString() }).ToList();
            selectList.Insert(0, sl);
            return selectList;
        }
        /// <summary>
        /// 获取物品列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetQueryTime(string personNo)
        {
            SelectListItem sl = new SelectListItem();
            sl.Selected = true;
            sl.Text = "请选择...";
            sl.Value = "0";
            if (!string.IsNullOrEmpty(personNo))
            {                                        //priorID!=0
                IList<Work_ArticleInfo> list = wa.GetAllCon_Article(" AND    id in (select ArticleId from dbo.Work_Post_Relation where postid in (select 岗位编号 from dbo.FILE_PERSONNEL_VIEW where personnelid='" + personNo + "'))");
                IList<SelectListItem> selectList =
                 list.Select(m => new SelectListItem() { Text = m.ArticleName.ToString(), Value = m.ID.ToString() }).ToList();
                selectList.Insert(0, sl);
                return selectList;
            }
            else
            {
                IList<SelectListItem> selectList = new List<SelectListItem>();
                selectList.Add(sl);
                return selectList;
            }
        }
        /// <summary>
        /// 获取规格列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetUnitList(int? id)
        {
           
                IDictionary<int, string> dic = new Dictionary<int, string>();
                dic.Add(0, "请选择...");
                IList<SelectListItem> selectList =
                 dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
                return selectList;
          
        }
       
        #endregion ----------------------------



    }
}
